/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import midlet.MidletPlay;

/**
 *
 * @author LTTDNQ
 */
public class RunableSound implements Runnable {

    private InputStream _is;
    private Player _p;

    private MidletPlay _midlet;

    public RunableSound(MidletPlay midlet) {
        this._midlet = midlet;
    }

    public void run() {

        try {
            while (!_midlet.getStopMusic()) {
                choiNhac();
                Thread.sleep(32000);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    public void tatNhac() {
        try {
            _p.stop();
            _p.close();
            _is.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    private void choiNhac() {
        _is = getClass().getResourceAsStream("/sound/soundbg.wav");
        try {
            _p = Manager.createPlayer(_is, "audio/X-wav");
            _p.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }
}
